﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace NRainel.Security
{
    /// <summary>
    /// 安全类
    /// </summary>
    public static class SecurityHelper
    {
        /// <summary>
        /// 用MD5算法对字符串加密
        /// </summary>
        /// <param name="strOriginal"></param>
        /// <returns></returns>
        public static string Md5EncryptString(string strOriginal)
        {
            var encoder = new UTF8Encoding();
            var md5Hasher = new MD5CryptoServiceProvider();
            byte[] bytePassword = md5Hasher.ComputeHash(encoder.GetBytes(strOriginal));
            string str = System.Text.Encoding.Default.GetString(bytePassword);
            return str;
        }

        /// <summary>
        /// 用MD5算法对字符串加密
        /// </summary>
        /// <param name="strOriginal"></param>
        /// <returns></returns>
        public static byte[] Md5Encrypt(string strOriginal)
        {
            var encoder = new UTF8Encoding();
            var md5Hasher = new MD5CryptoServiceProvider();
            byte[] bytePassword = md5Hasher.ComputeHash(encoder.GetBytes(strOriginal));
            return bytePassword;
        }

        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="strOriginal"></param>
        /// <returns></returns>
        public static string DESDecrypt(string strOriginal)
        {
            SymmetricAlgorithm mCSP = new DESCryptoServiceProvider();
            mCSP.Key = Convert.FromBase64String("Q41H6FzFCPw=");
            mCSP.IV = Convert.FromBase64String("3AzfCOPT6dw=");
            ICryptoTransform ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);
            byte[] bty = Convert.FromBase64String(strOriginal);
            var ms = new MemoryStream();

            var cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
            cs.Write(bty, 0, bty.Length);
            cs.FlushFinalBlock();
            cs.Close();
            string strDecrpyted = Encoding.UTF8.GetString(ms.ToArray());
            return strDecrpyted;
        }

        /// <summary>
        /// Sql和Script注入式攻击检查
        /// </summary>
        /// <param name="insertStr">需检查的语句</param>
        /// <returns>返回安全语句</returns>
        public static string CheckInsertAttack(string insertStr)
        {
            if (insertStr == null)
                return insertStr;
            string safeStr = insertStr.Split(new[] { "'", ";", " ", "<", "--" }, 1, StringSplitOptions.RemoveEmptyEntries)[0];

            return safeStr;
        }

        public static string SHA1Encrypt(string insertStr)
        {
            byte[] tmpByte;
            SHA1 sha1 = new SHA1CryptoServiceProvider();

            tmpByte = sha1.ComputeHash(GetKeyByteArray(insertStr));
            sha1.Clear();

            return GetStringValue(tmpByte);

        }

        public static string SHA256Encrypt(string insertStr)
        {
            byte[] tmpByte;
            SHA256 sha256 = new SHA256Managed();

            tmpByte = sha256.ComputeHash(GetKeyByteArray(insertStr));
            sha256.Clear();

            return GetStringValue(tmpByte);

        }

        public static string SHA512Encrypt(string insertStr)
        {
            byte[] tmpByte;
            SHA512 sha512 = new SHA512Managed();

            tmpByte = sha512.ComputeHash(GetKeyByteArray(insertStr));
            sha512.Clear();

            return GetStringValue(tmpByte);

        }

        private static byte[] GetKeyByteArray(string strKey)
        {
            ASCIIEncoding Asc = new ASCIIEncoding();

            int tmpStrLen = strKey.Length;
            byte[] tmpByte = new byte[tmpStrLen - 1];

            tmpByte = Asc.GetBytes(strKey);

            return tmpByte;
        }

        private static string GetStringValue(byte[] bytes)
        {
            string tmpString = "";

            ASCIIEncoding Asc = new ASCIIEncoding();
            tmpString = Asc.GetString(bytes);

            return tmpString;
        }

        private static string GetNumString(byte[] bytes)
        {
            string tmpString = "";
            for (int iCounter = 0; iCounter < bytes.Length; ++iCounter)
            {
                tmpString = tmpString + bytes[iCounter];
            }

            return tmpString;
        }

    }
}